<?php 
require 'header.php';

	if(isset($_POST['submit']))
	{
		echo "<h3>Employee information was updated<h3>";
		$query="UPDATE employee SET access_level='$_POST[level]', employee_name='$_POST[name]', department='$_POST[department]', hourly_salary='$_POST[salary]', branch='$_POST[branch]' WHERE ID=".$_GET['ID'];
		$result=mysqli_query ($con, $query);
	}
	
if(!isset($_GET['ID']))
{
	$query="SELECT employee_name, ID FROM employee";
	$result=mysqli_query ($con, $query);
	echo "<h3>Edit Employee</h3>";
	while($emp = mysqli_fetch_array($result))
	{	
		$link="editEmployee.php?ID=".$emp['ID'];
		echo "<br><a href=$link>$emp[employee_name]</a>";
	}
}
else
{

$query="SELECT name FROM branch";
$result=mysqli_query ($con, $query);
$query1="SELECT * FROM employee WHERE ID=".$_GET['ID'];
$result1 = mysqli_query($con, $query1);
$emp = mysqli_fetch_array($result1);
	
?>
<form action="" method="post"> <!--form to add new employees -->
        <table>
             <tr>
                <td>Access Level:</td>
            </tr>
            <tr>
                <td><select name="level">
				<?php
				$levels=array('Manager','Sales');
				foreach ($levels as $level)
				{
				$selected = ($level == $emp['access_level']) ? "Selected='selected'" : "";
				echo "<option $selected value=$level>$level</option>";
                       
				}
				?>
						
                    </select></td>
					
					
            </tr>
            <tr>
                <td>Name:</td>
            </tr>
            <tr>
                <td><input type="text" name="name" value="<?php echo $emp['employee_name']; ?>"></td>
            </tr>
			<tr>
                <td>Department:</td>
            </tr>
            <tr>
                <td><input type="text" name="department" value="<?php echo $emp['department']; ?>"></td>
            </tr>
			<tr>
                <td>Hourly Salary:</td>
            </tr>
			<tr>
                <td><input type="text" name="salary" value="<?php echo $emp['hourly_salary']; ?>"></td>
            </tr>
			<tr>
				<tr>
            <td>Branch:</td>
            </tr>
			<tr>
                <td><select name="branch">
                        <?php
						 while($branch = mysqli_fetch_array($result))
						 {
							$selected = ($branch['name'] == $emp['branch']) ? "Selected='selected'" : "";
                            echo "<option $selected value=$branch[name]>$branch[name]</option>";
						}
                        
					
						?>
                    </select></td>
            </tr>
		      
        
            <tr>
                <td><input type="submit" name="submit" value="Update Employee"></td>
            </tr>

        </table>
    </form>
	<?php
	}
	

	
	
	
	
	require 'footer.php';					
	?>
	
